package com.jinxin.platform.cdinfogo.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-12-28
 * @Author: Lingnan
 **/
@Slf4j
public class HttpUtils {
    /**
     *
     * @description: 从服务器获得一个输入流(本例是指从服务器获得一个image输入流)
     * @author: Jeff
     * @date: 2019年12月7日
     * @return
     */
    public static InputStream getInputStream(String urlPath) {
        InputStream inputStream = null;
        HttpURLConnection httpURLConnection = null;
        try {
            URL url = new URL(urlPath);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            // 设置网络连接超时时间
            httpURLConnection.setConnectTimeout(3000);
            // 设置应用程序要从网络连接读取数据
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("GET");
            int responseCode = httpURLConnection.getResponseCode();
            if (responseCode == 200) {
                // 从服务器返回一个输入流
                inputStream = httpURLConnection.getInputStream();
            }
        } catch (IOException e) {
           log.error(e.toString());
        }
        return inputStream;
    }

//    public static void main(String args[]) {
//        // 服务器图片url
//        String urlPath = "http://117.132.2.109:9002//upload/module/1640661098963/1640661098963/logo.png";
//        // 本地文件夹路径
//        String diskPath = "D:\\111\\images\\";
//        // 从服务器端获得图片，并保存到本地
//        FileUtils.saveImageToDisk(getInputStream(urlPath), diskPath);
//    }

}
